package com.wins.contact.domain;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Entity;

@Entity
@RooJavaBean
@RooToString
@RooEntity(identifierColumn = "id")
public abstract class AbstractAuthenticatedEntity {

    private String userName;

    public String getUserName() {
        if (userName == null) {
            userName = SecurityContextHolder.getContext().getAuthentication().getName();
        }
        return userName;
    }

	@Transactional
    public void persist() {
		getUserName();
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }
}
